<template>
  <div id="articalDetail" v-loading="$store.state.loading">
    <div class="back">
      <div class="back-content" @click="$router.back()">
        <i class="el-icon-arrow-left"></i>
        <span>返回</span>
      </div>
    </div>
    <div class="artical-detail-content" v-if="detail && !$store.state.loading">
      <div class="detail-title">{{ detail.title }}</div>
      <div class="detail-sub-title">
        <div class="author">
          <div class="avatar"><img :src="detail.user.avatar" alt="" /></div>
          <div class="user-info">
            <div class="name">{{ detail.user.name }}</div>
            <div class="create-time">{{ timestampToDate(detail.create_time).substring(0, 10) }}</div>
          </div>
        </div>
        <div class="op-list">
          <el-button type="warning" size="mini" @click="handleLikeClick"> <i class="el-icon-caret-top"></i>点赞 （{{ detail.like }}）</el-button>
          <el-button type="warning" size="mini" @click="handleHateClick">
            <i class="el-icon-caret-bottom"></i>
          </el-button>
        </div>
      </div>
      <!-- <div class="detail-cover"
           v-if="detail.cover">
        <img :src="detail.cover"
             alt="" />
      </div> -->
      <div class="detail-main" v-html="this.detail.description" v-highlight></div>

      <!-- <div class="artical-op">
        <div class="artical-op-artical prev-artical"><i class="el-icon-arrow-left"></i><span>上一章</span></div>
        <div class="artical-op-artical next-artical"><span>下一章</span><i class="el-icon-arrow-right"></i></div>
      </div> -->
    </div>
  </div>
</template>

<script>
import marked from 'marked'
export default {
  components() {},
  data() {
    return {
      detail: null
    }
  },
  created() {
    this.getDetail()
  },
  methods: {
    getDetail() {
      this.$store.commit('setLoading', true)
      this.$api
        .getBlogDetail({ uuid: this.$route.params.id })
        .then((res) => {
          if (res.code === 1) {
            this.detail = res.data

            this.detail.description = marked(this.detail.description, {
              sanitize: true
            })
          }
        })
        .then(() => {
          setTimeout(() => {
            this.$store.commit('setLoading', false)
          }, 300)
        })
    },

    handleLikeClick() {
      this.$api.setBlogLike({ uuid: this.detail.uu_id }).then((res) => {
        if (res.code === 1) {
          if (this.messageBox) {
            this.messageBox.close()
            this.messageBox = null
          }
          this.messageBox = this.$message.success('设置成功')

          this.detail.like = parseInt(this.detail.like) + 1
        }
      })
    },

    handleHateClick() {
      if (this.errorBox) {
        this.errorBox.close()
        this.errorBox = null
      }

      this.errorBox = this.$message.error('您忍心嘛！！')
    }
  }
}
</script>

<style lang="less" scoped>
#articalDetail {
  .artical-detail-content {
    padding: 30px;
    .detail-title {
      font-weight: bold;
      font-size: 20px;
    }

    .detail-sub-title {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-top: 25px;

      .author {
        display: flex;
        align-items: center;
        .avatar {
          width: 50px;
          height: 50px;

          img {
            width: 100%;
            height: 100%;
          }
        }

        .user-info {
          margin-left: 20px;
          font-size: 13px;
          .name {
          }
          .create-time {
            margin-top: 10px;
          }
        }
      }
    }

    .detail-main {
      min-height: 400px;
      margin-top: 35px;
      font-family: Menlo;
      line-height: 1.5;
    }
  }

  .detail-cover {
    margin-top: 30px;
    img {
      width: 100%;
      height: auto;
    }
  }

  .artical-op {
    display: flex;
    justify-content: space-between;
    margin-top: 60px;

    .artical-op-artical {
      display: flex;
      align-items: center;
      span {
        font-size: 14px;
        margin: 0 10px;
      }
    }
  }
}
</style>

